package com.cupcorn.balapkarung.state;

import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.balapkarung.entity.MC;
import com.cupcorn.balapkarung.entity.Map;
import com.cupcorn.balapkarung.entity.World;
import com.cupcorn.lib.Utils;
import com.cupcorn.lib.display.DisplayAnimatedSprite;
import com.cupcorn.lib.display.DisplaySprite;
import com.cupcorn.lib.display.DisplayTmx;
import com.cupcorn.lib.game.GameAnimatedSprite;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameSprite;
import com.cupcorn.lib.game.GameState;
import com.cupcorn.lib.game.GameTiledSprite;

public class StateGamePlay extends GameState implements GameDefine
{
	DisplaySprite			displaySprite;
	DisplayAnimatedSprite	displayAnimatedSprite;
	DisplayTmx				displayTmx;
	
	// GameSprite[] gameSprite = new GameSprite[GAME_PLAY_SPR.length];
	// GameTiledSprite[] gameTiledSprites = new
	// GameTiledSprite[GAME_PLAY_MC.length];
	// GameAnimatedSprite[] gameAnimatedSprites = new
	// GameAnimatedSprite[GAME_PLAY_MC.length];
	TMXTiledMap				tiledMap;
	
	MC						mc;
	World					world;
	Map						map;
	
	public StateGamePlay(GameEngine engine)
	{
		super(engine);
	}
	
	public void load()
	{
		displayTmx = new DisplayTmx(engine, GAME_PLAY_TMX);
		displaySprite = new DisplaySprite(engine, GAME_PLAY_SPR);
		displayAnimatedSprite = new DisplayAnimatedSprite(engine, GAME_PLAY_MC);
	}
	
	public void init()
	{
		tiledMap = displayTmx.newTiledMap(0);
		
		for (int i = 0; i < GAME_PLAY_SPR.length; i++)
		{
			displaySprite.newSprite(i, engine.hud);
		}
		
		for (int i = 0; i < GAME_PLAY_MC.length; i++)
		{
			displayAnimatedSprite.newAnimatedSprite(i);
		}
		
		engine.hud.registerTouchArea(displaySprite.getSprite(GPS_BTN_JUMP));
		
		mc = new MC(displayAnimatedSprite.getAnimatedSprite(0));
		map = new Map(engine, tiledMap, mc);
		world = new World(engine, mc, map);
		
		Utils.TRACE("map.getWidth()" + tiledMap.getWidth());
		Utils.TRACE("map.getTileWidth()" + tiledMap.getTileWidth());
		
		engine.camera.setBounds(tiledMap.getMapOriginX(), tiledMap.getMapOriginY(), tiledMap.getWidth() * tiledMap.getTileWidth(), tiledMap.getHeight() *
			tiledMap.getTileHeight());
		engine.camera.setBoundsEnabled(true);
		engine.camera.setZoomFactor(engine.getGameHeight() / 480);
		engine.camera.setChaseEntity(displayAnimatedSprite.getAnimatedSprite(0));
	}
	
	public void update(float deltaTime)
	{
		mc.onUpdate(deltaTime);
		world.onUpdate(deltaTime);
	}
	
	public void exit()
	{	
		
	}
	
	public boolean keyUp(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			engine.finish();
		}
		return false;
	}
	
	public void sceneTouch(Scene scene, TouchEvent touchEvent)
	{	
		
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if (pSceneTouchEvent.isActionUp() && pTouchArea.equals(displaySprite.getSprite(GPS_BTN_JUMP)))
		{
			Utils.TRACE("click");
			mc.switchState(mc.JUMP);
		}
		return false;
	}
	
	public void click(ClickDetector clickDetector, int pointerID, float sceneX, float sceneY)
	{
		// if(Utils.isOnArea(sceneX* engine.camera.getZoomFactor(), sceneY *
		// engine.camera.getZoomFactor(), gameSprite[0])){
		// Utils.TRACE("click");
		// mc.switchState(mc.JUMP);
		// }
	}
	
	public void scrollStarted(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void Scroll(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
	public void scrollFinished(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{	
		
	}
	
}
